<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="form" :model="queryParams" :inline="true">
        <el-form-item prop="keywords" label="选项名称">
          <el-input
            v-model="queryParams.keywords"
            placeholder="选项名称"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery" icon="el-icon-search">搜索</el-button>
          <el-button @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never">
      <template #header>
        <el-button type="success" @click="exportExcel"><i class="el-icon-document-checked"></i> 导出问卷回答</el-button>
      </template>

      <el-table
        v-loading="loading"
        :data="dataList"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" width="55" prop="dataIndex"/>
        <el-table-column label="姓名" prop="name"/>
        <el-table-column label="电话" prop="phone"/>
        <el-table-column label="填写时间" prop="createTimeText"/>
        <el-table-column fixed="right" label="操作"  min-width="220" width="220">
          <template #default="scope">
            <el-button
              type="success"
              size="small"
              link
              @click="openAnswerDialog(scope.row)"
            ><i class="el-icon-set-up"></i> 查看回答</el-button>
            <!-- <el-button
              type="danger"
              size="small"
              link
              @click="handleDelete(scope.row.id)"
            ><i class="el-icon-delete"></i> 删除</el-button> -->
          </template>
        </el-table-column>
      </el-table>

      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryParams.pageNum"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </el-card>

    <el-dialog
      :visible.sync="dialog.visible"
      :title="dialog.title"
      width="800px"
      class="answerDetail"
      :before-close="closeDialog"
    >
      <div v-for="page in checkedData" :key="page.id">
        <p style="margin-top: 10px;">{{page.title}}</p>
        <template  v-if="!page.questions">
          <div>
            <div>
              <span v-show="page.type == 'TEXTAREA'">{{page.answer}}</span>
              <span v-show="page.type == 'EXCLUSIVE_CHOICE'">{{page.answer.title}}</span>
              <template v-if="page.type == 'MULTIPLE_CHOICE'">
                <span v-for="item in page.answer" :key="item.id" style="margin-right: 5px;">{{item.title}}</span>
              </template>
            </div>
          </div>
        </template>
        <template  v-if="page.questions">
          <div v-for="(question, index) in page.questions" :key="question.id">
            <div>
              <span>{{index + 1}}、</span>
              <span>{{question.title}}</span>
            </div>
            <div>
              <span v-show="question.type == 'TEXTAREA'">{{question.answer}}</span>
              <span v-show="question.type == 'EXCLUSIVE_CHOICE'">{{question.answer.title}}</span>
              <template v-if="question.type == 'MULTIPLE_CHOICE'">
                <span v-for="item in question.answer" :key="item.id" style="margin-right: 5px;">{{item.title}}</span>
              </template>
            </div>
          </div>
        </template>
      </div>
    </el-dialog>

  </div>
</template>

<script>

import preventionAnswerApi from '@/api/aliyun/questionnaire/preventionAnswer'
import { formatDate } from '@/utils/formatTime'
export default {
	name: 'userIndex',
	data() {
		return {
			queryParams: {
        keywords: '',
        pageNum: 0,
        pageSize: 10,

      },
      total: 0,
      loading: false,
      dataList: [],
      ids: [],
      dialog: {},
      formData: {},
      rules: {},

      checkedData: {},
		};
	},
  methods: {
    exportExcel () {
      const loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      preventionAnswerApi.exportAnswers().then(res => {
        loading.close();
        const url = window.URL.createObjectURL(res)
        const link = document.createElement('a')
        link.style.display = 'none'
        link.href = url
        link.setAttribute('download', '问卷答案.xlsx')
        document.body.appendChild(link)
        link.click()
        document.body.removeChild(link)
        URL.revokeObjectURL(url)
      })
    },
    closeDialog () {
      this.$set(this.dialog, 'visible', false);
    },
    openAnswerDialog (row) {
      let contentJson = row.answerJson;
      if (contentJson) {
        contentJson = JSON.parse(contentJson)
      } else {
        contentJson = [];
      }
      this.checkedData = contentJson;
      this.$set(this.dialog, 'visible', true);
      this.$set(this.dialog, 'title', '查看回答');
    },
    handleQuery() {
      this.loading = true;
      let params = {
        number: this.queryParams.pageNum,
        size: this.queryParams.pageSize
      };
      preventionAnswerApi.page(params).then((res) => {
        let data = res.data;
        let that = this;
        let index = 1
        data.content.forEach((v) => {
          v.createTimeText = formatDate(new Date(v.createTime), "YYYY-mm-dd HH:MM:SS")
          v.dataIndex = index++;
        });
        this.dataList = data.content;
        
        this.total = data.count;
        this.loading = false;
      }).catch(() => {});
    },
    resetQuery() {
      this.queryParams = {
        keywords: '',
        pageNum: 0,
        pageSize: 10,
      };
      this.handleQuery();
    },
    handleDelete(id) {
      this.$confirm('此操作将永久删除该答案, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        if (id) {
          answerApi.delete(id).then((res) => {
            this.$message({message: '问卷答案删除成功！', type: 'success'});
            this.handleQuery();
          });
        } else {
          this.$message.error('暂不支持批量删除问卷答案！');
        }
      }).catch(() => {});
      
    },
    handleSelectionChange(val) {
      this.ids = [];
      val.forEach(v => {
        this.ids.push(v.id);
      });
    },
    handleSelectionRoleChange(rows) {
      if (rows) {
        let arr = [];
        rows.forEach((v) => {
          arr.push(v.id);
        });
        this.selectRoleIdArr = arr;
      }
    },
    handleSizeChange(val) {
      this.queryParams.pageSize = val;
      this.handleQuery();
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val;
      this.handleQuery();
    },
  },
  created() {
    this.handleQuery();
  }
};
</script>

<style scoped>

.answerDetail /deep/ .el-dialog__body {
    height: 80vh;
    overflow: auto;
}

.block {
  margin-top: 10px;
  float: right;
  margin-bottom: 10px;
}
</style>